home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / comms / other / rxsocket / examples / dtudpserv.rexx < prev    next >
OS/2 REXX Batch file  |  1999-06-14  |  3KB  |  115 lines

  1. /*
  2.     DTUDPServ.rexx - daytime/udp server
  3.  
  4.     Shows:
  5.     - how to write a mix inetd/stand-alone server
  6.     - how to write an udp server
  7.  
  8.     Simple daytime/udp server.
  9.     Run:
  10.     - as stan-dalone:
  11.         rx dtudpserv
  12.       to stop:
  13.         rx "shell DTUDPSERV    quit"
  14.     - as inetd service
  15.         be sure you copied rxs to c:
  16.         add to the services database:
  17.             daytime 13/udp
  18.         add to the inetd database:
  19.             daytime dgram udp nowait root c:rxs rxs dtudpserv
  20.         or
  21.             daytime dgram udp nowait root c:rxs rxs "dtudpserv SYSLOG"
  22.         if you want log
  23.  
  24.     Use daytimeudp.rexx as client.
  25. */
  26.  
  27. l="rexxsupport.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
  28. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
  29. l="rxsocket.library";if ~show("L",l) then;if ~addlib(l,0,-30) then do;say "can't find" l;exit;end
  30.  
  31. prg=ProgramName("NOEXT")
  32.  
  33. if ~RMH_ReadArgs("SYSLOG/S") then do
  34.     call  PrintFault(IoErr(),prg)
  35.     exit
  36. end
  37.  
  38. log=parm.0.flag
  39.  
  40. ls=LastSocket()
  41. if ls>=0 then do
  42.     call handle(ls)
  43.     exit
  44. end
  45.  
  46. portName=upper(prg)
  47. if ~OpenPort(portName) then call err "can't create port",1
  48. ps=PortSignal(portName)
  49.  
  50. sock=socket("INET","DGRAM")
  51. if sock<0 then call err "can't create socket"
  52.  
  53. local.addrFamily="INET"
  54. local.addrAddr=0
  55. if GetServByName("SE","daytime","udp") then
  56.     local.AddrPort=se.ServPort
  57. else local.AddrPort=13
  58.  
  59. if bind(sock,"LOCAL")<0 then call err "can't bind socket"
  60.  
  61. call info "opened"
  62.  
  63. sel.read.0=sock
  64. open=1
  65. do while open
  66.     res=WaitSelect("SEL",,,or(ps,2**12))
  67.     if res<0 then call err "wait error"
  68.     if and(sel.signals,2**12)~=0 then open=0
  69.     if res==1 then call handle(sock)
  70.     if and(sel.signals,ps)~=0 then do
  71.         pkt=GetPkt(portName)
  72.         if pkt~=Null() then do
  73.             comm=GetArg(pkt)
  74.             res=0
  75.             select
  76.                 when upper(comm)=="QUIT" then open = 0
  77.                 otherwise res=15
  78.             end
  79.             call Reply(pkt,res)
  80.         end
  81.     end
  82. end
  83. call info "closed"
  84. exit
  85.  
  86. /***************************************************************************/
  87. handle: procedure expose remote. log prg
  88. parse arg sock
  89.     if RecvFrom(sock,"dummy",1,,"REMOTE")<=0 then call info "recv error"
  90.     call info "connection from" remote.AddrAddr":"remote.AddrPort
  91.     call GetDate("D","GMT")
  92.  
  93.     d.0="Sun";d.1="Mon";d.2="Tue";d.3="Wed";d.4="Thu";d.5="Fri";d.6="Sat"
  94.     m.1="Jan";m.2="Feb";m.3="Mar";m.4="Apr";m.5="May";m.6="Jun";m.7="Jul";m.8="Aug";m.9="Sep";m.10="Oct";m.11="Nov";m.12="Dec"
  95.  
  96.     all=formatdate("D","%m %w %d %Y %H:%M:%S GMT")
  97.     parse var all i j rest
  98.     i=i%1
  99.     date=d.j"," m.i || rest"A"x
  100.     if SendTo(sock,date,0,"REMOTE")<length(data) then call info "recv error"
  101.     return
  102. /***************************************************************************/
  103. err: procedure expose log prg
  104. parse arg msg,ntdoerr
  105.     log=1
  106.     call info msg "("Errorstring()")"
  107.     exit
  108.  
  109. info: procedure expose log prg
  110. parse arg msg
  111.     if log then call SysLog(prg msg)
  112.     say prg":" msg
  113.     return
  114. /***************************************************************************/
  115.